package com.tansuo365.test1.service.kucunservice.kucunimpl;

import com.tansuo365.test1.bean.kucunbean.ChukuDetails;
import com.tansuo365.test1.bean.kucunbean.ChukuZong;
import com.tansuo365.test1.bean.kucunbean.WuliaoKucunRiZhi;
import com.tansuo365.test1.bean.otherbean.HuiShouZhixing;
import com.tansuo365.test1.mapper.kucunmapper.ChukuDetailsMapper;
import com.tansuo365.test1.mapper.kucunmapper.ChukuZongMapper;
import com.tansuo365.test1.mapper.kucunmapper.WuliaoKucunRiZhiMapper;
import com.tansuo365.test1.mapper.kucunmapper.WuliaoKucunSearMapper;
import com.tansuo365.test1.mapper.othermapper.HuiShouZhixingMapper;
import com.tansuo365.test1.service.kucunservice.ChukuZongService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 已加入save时的各自的单据保存和物料主表的更改以及物料日志表的新增
 */
@Service
public class ChukuZongServiceImpl implements ChukuZongService {

    @Resource
    private ChukuZongMapper chukuZongMapper;
    @Resource
    private ChukuDetailsMapper chukuDetailsMapper;
    @Resource
    private WuliaoKucunSearMapper wuliaoKucunSearMapper;
    @Resource
    private WuliaoKucunRiZhiMapper wuliaoKucunRiZhiMapper;
    @Resource
    private HuiShouZhixingMapper huiShouZhixingMapper;

    @Override
    public String getTodayMaxChukuDanNumber() {
        return chukuZongMapper.getTodayMaxChukuDanNumber();
    }

    @Override
    public List<ChukuZong> listChukuZong(ChukuZong chukuZong) {
        return chukuZongMapper.listChukuZong(chukuZong);
    }

    @Override
    public List<ChukuZong> listChukuZongKehuishou(ChukuZong chukuZong) {
        return chukuZongMapper.listChukuZongKehuishou(chukuZong);
    }

    @Override
    public List<ChukuZong> listChukuZongNotAllBack(ChukuZong chukuZong) {
        return chukuZongMapper.listChukuZongNotAllBack(chukuZong);
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return chukuZongMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int deleteBatchByPKArr(Integer[] ids) {
        return chukuZongMapper.deleteBatchByPKArr(ids);
    }

    @Override
    public int insertSelective(ChukuZong chukuZong) {
        return chukuZongMapper.insertSelective(chukuZong);
    }

    @Override
    @Transactional
    public int saveZongAndDetails(ChukuZong chukuZong, List<ChukuDetails> chukuDetailsList) {

        int test = -100;
        for (ChukuDetails chukuDetails : chukuDetailsList) {
            //查看物品在库存中是否不够出库数量
            Double chukushuliang = 0D;
            Double jieyu = 0D;
            try {
                String chukukufang = chukuDetails.getChukukufang();
                String mingchengguige = chukuDetails.getMingchengguige();
                chukushuliang = chukuDetails.getChukushuliang();
                jieyu = wuliaoKucunRiZhiMapper.checkOutTheNumberEnough(chukukufang, mingchengguige);

                if (chukushuliang > jieyu) {
                    return test;
                }
            } catch (Exception e) {
                e.printStackTrace();
                return test;
            }
            test--;
        }

        System.out.println("chuku save");
        int insertCode = 0;
        insertCode = chukuZongMapper.insertSelective(chukuZong);
        if (!(insertCode > 0)) {
            return 0;
        }
        Integer zongzhangId = chukuZong.getId();
        int i = 0;
        int upKu = 0;
        int inSertRizhi = 0;
        int inDetails = 0;

        int isKehuishou = 0;
        //保存每个出库单物料
        for (ChukuDetails chukuDetails : chukuDetailsList) {
            //设置对应总表id
            i++;
            chukuDetails.setZongzhangid(zongzhangId);
            inDetails += chukuDetailsMapper.insertSelective(chukuDetails);
//                upKu += wuliaoKucunSearMapper.updateWuliaoFachuShuliangByWuliaoName(chukuDetails.getMingchengguige(), chukuDetails.getChukushuliang(), chukuDetails.getChukukufang());
//            }
            String kehuishou = chukuDetails.getKehuishou();
            if(kehuishou.equals("可回收")){
                HuiShouZhixing huiShouZhixing = new HuiShouZhixing();
                //设置参数
                huiShouZhixing.setDuiyingchukudanhao(chukuZong.getWuliaochukudanhao());
                huiShouZhixing.setMingchengguige(chukuDetails.getMingchengguige());
                huiShouZhixing.setDanwei(chukuDetails.getDanwei());
                Double chukuhuishoushuliang = chukuDetails.getChukushuliang(); //出库数量(可回收的)
                Double tuikushuliang = 0.0;
                Double yihuishoushuliang = 0.0;
                //未回收量= 出库单中绑定的这条物料是可回收的 出库多少 - 用户填入的已回收量
                Double weihuishouliang = chukuhuishoushuliang;
                huiShouZhixing.setChukushuliang(chukuhuishoushuliang);
                huiShouZhixing.setHuishoushuliang(yihuishoushuliang);
                huiShouZhixing.setTuikushuliang(tuikushuliang);
                huiShouZhixing.setWeihuishoushuliang(weihuishouliang);
                huiShouZhixing.setLiangliaodanwei(chukuZong.getLingliaodanwei());
                huiShouZhixing.setLiangliaoren(chukuZong.getLingliaoren());
//            huiShouZhixing.setChukushijian(huiShouZong.get);
                String huishouzhuangtai = "未回收";
                huiShouZhixing.setHuishouzhuangtai(huishouzhuangtai);
                huiShouZhixingMapper.insertSelective(huiShouZhixing);
            }

            WuliaoKucunRiZhi wuliaoKucunRiZhi = new WuliaoKucunRiZhi();
            wuliaoKucunRiZhi.setMingchengguige(chukuDetails.getMingchengguige());
            wuliaoKucunRiZhi.setXiangmuweihu(chukuZong.getXiangmumingcheng()); //项目名
            wuliaoKucunRiZhi.setFachushuliang(chukuDetails.getChukushuliang()); //出库数量
            wuliaoKucunRiZhi.setFachuleixing("正常出库"); //发出类型
            wuliaoKucunRiZhi.setDanwei(chukuDetails.getDanwei()); //单位
            wuliaoKucunRiZhi.setRukukufang(chukuDetails.getChukukufang()); //库房
            wuliaoKucunRiZhi.setWuliaobianma(chukuDetails.getWuliaobianma());
            wuliaoKucunRiZhi.setDalei(chukuDetails.getDalei());
            inSertRizhi += wuliaoKucunRiZhiMapper.insertSelective(wuliaoKucunRiZhi);
//            WuliaoKucunRiZhi wuliaoKucunRiZhi = new WuliaoKucunRiZhi();
        }
        if (!(i == chukuDetailsList.size() && i != 0)) {
            return 0;
        } else if (!(i == inDetails)) {
            //update的出库数和条目不一样多,那么就说明有的物料没执行update成功,或者是因为库存没有这个物料
            return -1;
        }
        if (!(inSertRizhi == chukuDetailsList.size())) {
            return 0;
        }
        return i; //返回的是插入出库详情物料的数量
    }

    @Override
    public ChukuZong selectByPrimaryKey(Integer id) {
        return chukuZongMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(ChukuZong chukuZong) {
        return chukuZongMapper.updateByPrimaryKeySelective(chukuZong);
    }

    @Override
    public List<String> listChukudanhaos(Integer[] ids) {
        return chukuZongMapper.listChukudanhaos(ids);
    }

    @Override
    public int updateCanDeleteNotBindTuiku(String chukudan) {
        return chukuZongMapper.updateCanDeleteNotBindTuiku(chukudan);
    }
}
